Package org.javacommerce.google.servlet

Source Code of org.javacommerce.google.servlet.CallbackServlet

package org.javacommerce.google.servlet;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;
import org.javacommerce.google.handler.HandlerException;
import org.javacommerce.google.handler.HandlerFactory;
import org.javacommerce.google.ws.API;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.google.checkout.NotificationAcknowledgment;

/**
*
* @author Michael Blanton (mike@mikeblanton.com)
* @web.servlet name="Callback"
* @web.servlet-mapping
*   url-pattern="/Callback"
*/
public class CallbackServlet extends HttpServlet {

  private static final Log LOG = LogFactory.getLog(CallbackServlet.class);
  /**
   *
   */
  private static final long serialVersionUID = 4557328827383959815L;
 
  private static String NOTIFICATION_ACK;

  /* (non-Javadoc)
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  protected void doPost(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
    // Validate the Authorization header
    String authorization = _request.getHeader("Authorization");
    if (authorization == null || !authorization.equals(API.getAuthorizationHeader())) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Unauthorized access attempt from [" + _request.getRemoteAddr() + "].");
      }
      _response.setStatus(HttpStatus.SC_UNAUTHORIZED);
      return;
    }
   
    try {
      // Authorization matches, grab the request
      SAXBuilder builder = new SAXBuilder();
      Document xmlDoc = builder.build(_request.getReader());
      HandlerFactory.getNotificationHandler(xmlDoc).handleMessage(xmlDoc);
      _response.addHeader("Content-Type", "application/xml");
      _response.addHeader("Accepts", "application/xml");
      _response.getWriter().write(NOTIFICATION_ACK);
      _response.setStatus(HttpStatus.SC_OK);
    } catch (JDOMException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("JDOMException processing Google Callback: " + e.getLocalizedMessage(), e);
      }
      _response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    } catch (IOException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("IOException processing Google Callback: " + e.getLocalizedMessage(), e);
      }
      _response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    } catch (HandlerException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("IOException processing Google Callback: " + e.getLocalizedMessage(), e);
      }
      _response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    } catch (InstantiationException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("InstantiationException processing Google Callback: " + e.getLocalizedMessage(), e);
      }
      _response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    } catch (IllegalAccessException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("IllegalAccessException processing Google Callback: " + e.getLocalizedMessage(), e);
      }
      _response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    } catch (ClassNotFoundException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("ClassNotFoundException processing Google Callback: " + e.getLocalizedMessage(), e);
      }
      _response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
  }

  /* (non-Javadoc)
   * @see javax.servlet.GenericServlet#init()
   */
  public void init() throws ServletException {
    super.init();
    try {
      StringWriter writer= new StringWriter();
      Marshaller.marshal(new NotificationAcknowledgment(), writer);
      NOTIFICATION_ACK = writer.toString();
    } catch (MarshalException e) {
      if (LOG.isFatalEnabled()) {
        LOG.fatal("MarshalException caught while initializing servlet: " + e.getLocalizedMessage(), e);
      }
      throw new ServletException(e.getLocalizedMessage(), e);
    } catch (ValidationException e) {
      if (LOG.isFatalEnabled()) {
        LOG.fatal("ValidationException caught while initializing servlet: " + e.getLocalizedMessage(), e);
      }
      throw new ServletException(e.getLocalizedMessage(), e);
    }
  }

}
TOP

Related Classes of org.javacommerce.google.servlet.CallbackServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.